<form class="dwz-list-form page-wrap">
	<input type="hidden" name="pageNum" value="1">
	<header>
		<div class="toolbar">
			<div class="header-title">蓝牙设备</div>
		</div>
	</header>

	<section>

		<div class="scroll-content dwz-scroll-list">
			<div class="scroll">
				<div class="pullDown">
					<span class="pullDownIcon">&nbsp;</span>
					<span class="pullDownLabel" data-flip="松开刷新">下拉刷新</span>
				</div>

				<div class="empty_box">
					<div class="empty-img">
						<img src="image/browse-empty-bg.svg">
					</div>
					<p class="txt-info is-center">暂时还没有数据哦~</p>
				</div>

				<ul class="dwz-list pic-list dwz-list-box">

				</ul>

			</div>
			<a href="javascript:void(0)" class="top-btn hide">回顶部</a>
		</div>

	</section>
</form>

<script id="tpl_list" type="text/html">
	{{each list as item}}
	<li class="item dwz-ctl-hover" data-id="{{item.id}}" onclick="$.actionSheet.open({
		title:'{{item.name}}',
		buttons: ['设置密码', '查看详情', '删除']
	}, function(ret){
		$.alert.toast('buttonIndex: ' + ret.buttonIndex);

		if (ret.buttonIndex == 1) {
			var ble = api.require('ble');
			ble.writeValueForCharacteristic({
				peripheralUUID: '{{item.uuid}}',
				value: 'AAA101220A1111111144'
			}, function(ret) {
				if (ret) {
					api.alert({ msg: JSON.stringify(ret) });
				}
			});
		}
	})">
		<div class="item-content">
			<div class="txt-h3 txt-line1 flex-1">name：{{item.name}}</div>
			<div class="txt-info txt-line1">uuid：{{item.uuid}}</div>
			<div class="txt-info txt-line1">advertisingName：{{item.advertisingName}}</div>
			<div class="txt-info txt-line1">rssi：{{item.rssi}}</div>
			<!-- <div class="txt-info">manufacturerData：{{item.manufacturerData}}</div> -->
		</div>
	</li>
	{{/each}}
</script>



<script type="text/javascript">
	function helper(tpl, params) {
		// console.log(this, tpl, params)

		var html = template.render(tpl.html, {
			UserInfo: UserInfo
		});
		this.html(html).initUI();

		var $form = this,
			$listBox = this.find('ul.dwz-list-box');
		var ble = api.require('ble');

		var $form = this.find('form.dwz-list-form'),
			$listBox = $form.find('ul.dwz-list-box');
		var _timer = null;
		var _getPeripheral = function() {
			ble.getPeripheral(function(ret) {
				// console.log(JSON.stringify(ret))
				ret.peripherals.forEach(function(item) {
					console.log(item.name + ' ' + item.uuid + ' ### ' + item.rssi);
				});

				$form.listTotal(ret.peripherals.length, ret.peripherals);
				if ($form.total) {
					$form.find('.empty_box').hide();
				}

				var _html = template.render(tpl.tpl_list, {
					list: ret.peripherals
				});

				$listBox.html(_html).initUI();
			});
		}
		$form.requestList = function() {

			ble.initManager({
				single: true
			}, function(ret) {
				if (ret.state == "poweredOff") {
					$.alert.error({
						msg: "未开启蓝牙！"
					});
					ble.openBluetooth();
				} else if (ret.state == "unauthorized") {
					$.alert.error({
						msg: "蓝牙未授权！"
					});
				} else if (ret.state == "poweredOn") {

					ble.isScanning(function(ret) {});
					ble.scan({
						serviceUUIDs: []
					}, function(ret) {
						if (ret.status) {
							if (_timer) {
								clearInterval(_timer);
								_timer = null;
							}
							_timer = setInterval(_getPeripheral, 5000);
							setTimeout(_getPeripheral, 500);
						}
					});
				}
			});
		};

		$.listForm($form);

	}
</script>